package com.eviac.blog.mongo;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoDBClient {

	public static void main(String[] args) {

		try {

			Mongo mongo = new Mongo("localhost", 27017);

			DB db = mongo.getDB("zte");
			// 查询所有的Database
			for (String name : mongo.getDatabaseNames()) {
				System.out.println("dbName: " + name);
			}
			for (String name : db.getCollectionNames()) {
				System.out.println("collectionName: " + name);
			}
			
			DBObject user = new BasicDBObject();
		    user.put("name", "hoojo");
		    user.put("age", 24);
		    
		    DBCursor cursor = db.getCollection("a").find(user);
			while (cursor.hasNext()) {
				System.out.println(cursor.next().get("_id"));
			}
		    
		    
		    //db.getCollection("a").save(user);

			/*
			 * DB db = mongo.getDB("abc");
			 * 
			 * //查询所有的Database for (String name : mongo.getDatabaseNames()) {
			 * System.out.println("dbName: " + name); }
			 * 
			 * //查询所有的聚集集合 for (String name : db.getCollectionNames()) {
			 * System.out.println("collectionName: " + name); }
			 * 
			 * DBCollection collection = db.getCollection("foo");
			 * 
			 * BasicDBObject employee = new BasicDBObject();
			 * employee.put("name", "Hannah"); employee.put("no", 2);
			 * 
			 * collection.insert(employee);
			 * 
			 * BasicDBObject searchEmployee = new BasicDBObject();
			 * searchEmployee.put("id", 1);
			 * 
			 * DBCursor cursor = collection.find(searchEmployee);
			 * 
			 * while (cursor.hasNext()) { System.out.println(cursor.next()); }
			 * 
			 * DB db2 = mongo.getDB("zte"); DBCollection c =
			 * db2.getCollection("rs"); if(!db2.collectionExists("rs")) {
			 * DBObject options = new BasicDBObject(); options.put("size", 20);
			 * options.put("capped", 20); options.put("max", 20);
			 * //print(db.createCollection("account", options));
			 * System.out.println(db.createCollection("account", options)); }
			 * 
			 * System.out.println("The Search Query has Executed!");
			 */

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}

	}

}